<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <!-- 找到数组中所有的孤独数字 -->
    <script>
      var nums = [0, -2, -3, -4, -5, -6]
      var map = new Map()
      //用哈希表保存数组里的每一个数为键，若是首次创建，值为0，否则值+1
      for (var num of nums) {
        if (map.has(num)) {
          map.set(num, map.get(num) + 1)
        } else {
          map.set(num, 0)
        }
      }
      var res = []
      //遍历哈希表，找出值为0，且num+1和num-1都不是键的数
      for (var [num, count] of map) {
        if (count == 0 && !map.has(num + 1) && !map.has(num - 1)) {
          res.push(num)
        }
      }
      console.log(res.toString())
    </script>
  </body>
</html>
